import { reactive, ref, nextTick } from 'vue'
import { EditType, Title } from '@/type/BaseEnum';
import { ExperienceSaveApi, ExperienceRemoveApi, ExperienceUpdateApi, ExperienceDetailApi } from '@/api/Experience/ExperienceList'
import useInstance from '@/hooks/useInstance';
import { Result, StatusCode } from '@/utils/request';
export default function addEdit(tableDate?, getExperiencePage?, detailBtn?) {
  const RefMyExperience = ref<{ show: (type: string, row?: any) => void }>()
  const { global } = useInstance()
  const dialogModel = reactive({
    experienceName: '',
    description: '',
    wordFiles: [] as any,
    createId: '1',
    id: '',
    auditStatus: '0',
  })
  const rules = reactive({
    experienceName: [{
      required: true,
      message: '请填写经验名称',
      trigger: 'blur',
    }],
  })
  const addBtn = () => {
    RefMyExperience.value?.show(EditType.ADD)
  }
  const editBtn = async (row) => {
    
    console.log(row, 'row');
    const res = await ExperienceDetailApi({ id: row.id })
    if (res.code === 200) {
      dialogModel.id = res.data.id
    }
    RefMyExperience.value?.show(EditType.EDIT, res.data)
    console.log(dialogModel);
  }
  const deleteBtn = async (id) => {
    let parm = {
      id: id
    }
    const confirm = await global.$myconfirm('确定删除该数据吗?')
    if (confirm) {
      //执行删除操作
      let res = await ExperienceRemoveApi(parm)
      if (res && res.code == StatusCode.Success) {
        //信息提示
        global.$message({ message: '删除成功', type: 'success' })
        //刷新表格
        getExperiencePage();
      }
    } else {
      global.$message({ message: '取消删除', type: 'warning' })
    }
  }
  const save = async (dialogData) => {
    console.log(dialogData);
    if (dialogData.id) {
      console.log(dialogData.id, '----');

      const formData = new FormData();
      formData.append('experienceName', dialogData.experienceName)
      formData.append('description', dialogData.description)
      formData.append('createId', '1')
      formData.append('id', dialogData.id)
      formData.append('auditStatus', '0')

      if (dialogData.wordFiles && dialogData.wordFiles.length > 0) {
        dialogData.wordFiles.forEach((item) => {
          formData.append('wordFiles', item)
        })
      } else {
        // formData.append('wordFiles', '[]')
      }
      console.log(formData);
      const res = await ExperienceUpdateApi(formData)
      if (res.code === 200) {
        global.$message({ message: '修改成功', type: 'success' })
        // 刷新列表
        tableDate.search.page = 1
        getExperiencePage()
      }
    } else {
      console.log(dialogData.id, '++++++')

      console.log(dialogData.wordFiles, 'dialogData');
      const formData = new FormData();
      formData.append('experienceName', dialogData.experienceName)
      formData.append('description', dialogData.description)
      formData.append('createId', '1')
      formData.append('auditStatus', '0')
      console.log(dialogData.wordFiles, '====');

      if (dialogData.wordFiles && dialogData.wordFiles.length > 0) {
        dialogData.wordFiles.forEach((item) => {
          formData.append('wordFiles', item)
        })
      } else {
        // formData.append('wordFiles', '[]')
      }
      const res = await ExperienceSaveApi(formData)
      if (res.code === 200) {
        global.$message({ message: '新增成功', type: 'success' })
        // 刷新列表
        tableDate.search.page = 1
        getExperiencePage()
      }
    }
    // 怎么处理

  }
  const confirmEvent = async (row) => {
    const params = {
      auditStatus: 1,
      id: row.id
    }
    const res = await ExperienceUpdateApi(params)
    if (res.code === 200) {
      global.$message({ message: '审核成功', type: 'success' })
      getExperiencePage()
    }
  }
  const cancelEvent = async (row) => {
    const params = {
      auditStatus: 2,
      id: row.id
    }
    const res = await ExperienceUpdateApi(params)
    if (res.code === 200) {
      global.$message({ message: '审核驳回', type: 'error' })
      getExperiencePage()
    }
  }
  return {
    dialogModel, rules, addBtn, editBtn, deleteBtn, RefMyExperience, save, cancelEvent, confirmEvent
  }
}